/*
 * Copyright (c) 2010, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * kiddie-os
 * Created on: 18.04.2010
 *     Author: artur
 *
 */

#include <Object.h>

extern "C"
void *memcpy(void *dest, const void *src, size_t n)
{
    char *d = (char *)dest;
    char *s = (char *)src;
    while(n-- > 0) *d++ = *s++;
    return dest;
}

Object::Object()
{
  // TODO Auto-generated constructor stub

}

Object::~Object()
{
  // TODO Auto-generated destructor stub
}

/**
 * @fn Object &Object::operator +=(const Object & b)
 * @brief This adds attributes from object 'b' to Object.
 *
 */
Object &
Object::operator +=(Object & b)
{
  return *this;
}

/**
 * @fn Object &Object::operator +=(const Object & b)
 * @brief This adds attributes from object 'b' to Object.
 *
 */
Object &
Object::operator -=(Object & b)
{
  return *this;
}

Object &
Object::operator &=(const Object & b)
{
  return *this;
}

Object &
Object::operator |=(const Object & b)
{
  return *this;
}

Object &
Object::operator ^=(const Object & b)
{
  return *this;
}

Object
Object::operator |(const Object & b) const
{
  Object a = *this;
  a |= b;
  return a;
}

Object
Object::operator &(const Object & b) const
{
  Object a = *this;
  a &= b;
  return a;
}

Object
Object::operator ^(const Object & b) const
{
  Object a = *this;
  a ^= b;
  return a;
}

Object *
Object::locate(char *pattern)
{
  Object *response = new Object();
  return response;
}

Object *
Object::locate(Object *example)
{
  Object *response = new Object();
  return response;
}

/**
 *
 *
 *
 *
 *
 */
uint32_t
Reader::read32(size_t off)
{
  char c = 0;
  int ret1;
  int ret2;
  ret1 = read32blk(off);
  ret2 = read32blk(off + sizeof(uint32_t) - 1);
  c = (off % sizeof(uint32_t)) * 8;

  if (c)
    {
      ret1 >>= c;
      ret2 <<= (sizeof(uint32_t) * 8 - c);
    }
  ret1 |= ret2;
  return ret1;
}

uint32_t
Reader::read16(size_t off)
{
  int ret = read32(off);
  ret &= 0xffff;
  return ret;
}

uint32_t
Reader::read8(size_t off)
{
  int ret = read32(off);
  ret &= 0xff;
  return ret;
}

uint32_t
Reader::read32()
{
  int ret = read32(cur);
  cur += sizeof(uint32_t);
  return ret;
}

uint32_t
Reader::read16()
{
  int ret = read16(cur);
  cur += sizeof(u16);
  return ret;
}

uint32_t
Reader::read8()
{
  int ret = read8(cur);
  cur++;
  return ret;
}

void
Reader::jmp(int s)
{
  cur += s;
}

/**
 *
 *
 *
 *
 *
 */
BEReader::BEReader(IReader *r) :
  src(r), cur(0)
{
}

BEReader::~BEReader()
{
}

uint32_t
BEReader::read32(size_t off)
{
  int ret1;
  int ret2;
  char *val1 = (char *) &ret1;
  char *val2 = (char *) &ret2;
  ret1 = this->src->read32(off);

  val2[0] = val1[3];
  val2[1] = val1[2];
  val2[2] = val1[1];
  val2[3] = val1[0];

  return ret2;
}

uint32_t
BEReader::read16(size_t off)
{
  int ret1;
  int ret2;
  char *val1 = (char *) &ret1;
  char *val2 = (char *) &ret2;
  ret1 = this->src->read32(off);

  val2[0] = val1[1];
  val2[1] = val1[0];

  ret2 &= 0xffff;

  return ret2;
}

uint32_t
BEReader::read8(size_t off)
{
  int ret1;
  int ret2;
  char *val1 = (char *) &ret1;
  char *val2 = (char *) &ret2;
  ret1 = this->src->read32(off);

  val2[0] = val1[0];

  ret2 &= 0xff;

  return ret2;
}

uint32_t
BEReader::read32()
{
  int ret = this->read32(cur);
  cur += 4;
  return ret;
}

uint32_t
BEReader::read16()
{
  int ret = this->read16(cur);
  cur += 2;
  return ret;
}

uint32_t
BEReader::read8()
{
  int ret = this->read8(cur);
  cur++;
  return ret;
}

void
BEReader::jmp(int s)
{
  cur += s;
}
